<?php

class Quantri_HangHoaDVNgayController extends Zend_Controller_Action
{

    public function init()
    {
        $option=array(
        		"layout" => "layout",
        		"layoutPath" => APPLICATION_PATH."/layouts/scripts/quantri"
        );
        Zend_Layout::startMvc($option);
        
        $this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
    }

    public function indexAction()
    {
		$filter_ob = $this->_request->getParam("filter", "null");
		if($filter_ob == 'Nhomhang')
		{
			$id = $this->_request->getParam("IdNhomhang", "-1");
			$obHanghoadv = new Quantri_Model_Hanghoadv();			
		}
		else
		{
			$hanghoadvngay = new Quantri_Model_HangHoaDVNgay();
			$rsHanghoadvngay = $hanghoadvngay->GetAll();	
    	
			$this->view->rsHanghoadvngay = $rsHanghoadvngay;
		}
    }

    public function saveAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $hanghoadvngay = new Quantri_Model_HangHoaDVNgay();
        
        $idHanghoadvngay = $this->_request->getParam("idHanghoadvngay", "-1");
        $ngay = $this->_request->getParam("ngay","");
        $idhanghoadv = $this->_request->getParam("idhanghoadv","");
        $soluong = $this->_request->getParam("soluong","");      
        $trangthai = $this->_request->getParam("trangthai","off");
        $lstHanghoa = $this->_request->getParam("lstHanghoa","");
		
        if($trangthai=="on")
            $trangthai = 1;
        else
            $trangthai = 0;
        
        $_ngay = explode('/', $ngay);
        
		//echo json_encode($_ngay); die;
		
        $hanghoadvngay->Id = $idHanghoadvngay;
        $hanghoadvngay->Ngay = $_ngay[2] . '-' . $_ngay[1] . '-' . $_ngay[0];
        $hanghoadvngay->TrangThai = $trangthai;
        
		foreach($lstHanghoa as $hanghoa)
		{
			$hanghoadvngay->IdHangHoaDV = $hanghoa;
			
			$soluong = $this->_request->getParam("soluong_$hanghoa", "0");
			
			$hanghoadvngay->SoLuong = $soluong;
			
			$hanghoadvngay->save();
		}
    }

    public function saveupdateAction()
    {
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    
    	$hanghoadvngay = new Quantri_Model_Hanghoadvngay();
    
    	$Id = $this->_request->getParam("idHanghoadvngay", "-1");
    	$SoLuong = $this->_request->getParam("soluong","");    	
    	$TrangThai = $this->_request->getParam("TrangThai","off");    
    
    	if($TrangThai=="on")
    		$TrangThai = 1;
    	else
    		$TrangThai = 0;    
    
    	$hanghoadvngay->Id = $Id;
    	$hanghoadvngay->SoLuong = $SoLuong;    	
    	$hanghoadvngay->TrangThai = $TrangThai;
    	$hanghoadvngay->saveupdate();
    }
    
    public function xoaAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $hanghoadvngay = new Quantri_Model_HangHoaDVNgay();
        $id = $this->_request->getParam("id", "-1");
        
        $hanghoadvngay->xoa($id);
    }

    public function suaAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);       
        
        $idhanghoadvngay = $this->_request->getParam("id","-1");
        
        $hanghoadvngay = new Quantri_Model_HangHoaDVNgay();
        $hanghoadvngay->Id = $idhanghoadvngay;
        
        //$rshanghoadvngay = $hanghoadvngay->GetById(); 
        $rshanghoadvngay = $hanghoadvngay->GetFullHanghoadv();
        
        echo json_encode($rshanghoadvngay);
    }

    public function updateTrangthaiAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $hanghoadvngay = new Quantri_Model_HangHoaDVNgay();
        $id = $this->_request->getParam("id", "-1");
        $trangthai = $this->_request->getParam("id", "-1");
        
        if($trangthai==-1)
            return;
        
        $trangthai = ($trangthai==1) ? '0' : '1';
        
        $hanghoadvngay->Trangthai = $trangthai;
        $hanghoadvngay->UpdateTrangthai();
    }

    public function themAction()
    {
        $hanghoadv = new Quantri_Model_Hanghoadv();
        $rsHanghoadv = $hanghoadv->GetAll();
         
        $this->view->rsHanghoadv = $rsHanghoadv;
        
    	$nhomhhdv = new Quantri_Model_Nhomhanghoadv();
    	$this->view->rsNhomhhdv = $nhomhhdv->GetAll()->toArray();
    }
	

}





